<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型">
          <a-select style="width: 100%" v-decorator="['flag', {}]" @change="onChange">
            <a-select-option value="plan">
              事前申请
            </a-select-option>
            <a-select-option value="reimb">
              事后报销
            </a-select-option>
            <a-select-option value="other">
              其他
            </a-select-option>
          </a-select>


        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="关联的动态表单">
          <a-select style="width: 100%" v-decorator="['formId', {}]">
            <a-select-option v-for="item in data" :value="item.id">
              {{item.rname}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型标识">

          <a-select v-if="show" style="width: 100%" v-decorator="['formType', {}]">
            <a-select-option v-for="item in expenditureData" :value="item.id">
              {{item.name}}
            </a-select-option>
          </a-select>

          <a-input v-else placeholder="请输入类型标识" v-decorator="['formType', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <span slot="label">报表头部按钮
            <a-popover title="提示" trigger="click" placement="topRight">
              <template slot="content">
                <p>选择相应的按钮，则报表头部会展示相应的按钮；如果一个都不选，则报表头部不会展示工具栏</p>
                <p>建议选择：在线打印、PDF在线打印、PDF在线预览、导出PDF、预览</p>
              </template>
              <a href=""><a-icon type="question-circle" /></a>
            </a-popover>
          </span>
          <a-select mode="multiple" style="width: 100%" v-model="reportHeaderButton">
            <a-select-option value="1">在线打印</a-select-option>
            <a-select-option value="2">PDF在线打印</a-select-option>
            <a-select-option value="3">PDF在线预览</a-select-option>
            <a-select-option value="4">导出PDF</a-select-option>
            <a-select-option value="5">导出WORD</a-select-option>
            <a-select-option value="6">导出EXCEL</a-select-option>
            <a-select-option value="7">分页导出EXCEL</a-select-option>
            <a-select-option value="8">分Sheet导出EXCEL</a-select-option>
            <a-select-option value="9">预览</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import { getAction } from '../../../api/manage'

  export default {
    name: "Ureport2ConfigModal",
    data () {
      return {
        show:true,
        expenditureData:[],
        data:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/ureport2/ureport2Config/add",
          edit: "/ureport2/ureport2Config/edit",
        },
        reportHeaderButton:[],//报表按钮选择数组
      }
    },
    created () {

    },
    methods: {
      onChange(value){
        if(value=='other'){
          this.show=false
        }else{
          this.show=true
        }
        this.model.formType=undefined
        this.$forceUpdate()
      },
      init(){
        getAction('/ureport2/ureport2/listAll').then(res=>{
          if(res.success){
            this.data = res.result
          }
        })
        getAction('/base/tpExpenditureAllocationInfo/queryAll').then(res=>{
          if(res.success){
            this.expenditureData = res.result
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.init()
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.reportHeaderButton = (record.reportHeaderButton && record.reportHeaderButton != '0') ? record.reportHeaderButton.split(',') : []
        if(this.model.flag=='other'){
          this.show=false
        }else{
          this.show=true
        }
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'flag','formId','formType','delFlag'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.reportPrintButton = []
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            if(this.reportHeaderButton && this.reportHeaderButton.length>0){
              formData.reportHeaderButton = this.reportHeaderButton.join(',')
            }else{
              formData.reportHeaderButton = '0'
            }
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>